package de.nabble.model;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import de.nabble.gui.BoardPanel;
import de.nabble.gui.ColorTheme;

/**
 * Represents one letter with its points.
 * 
 * @author Tom
 * 
 */
public class Letter implements Cloneable {

	private final int points;
	private final char value;
	private final int count;
	private boolean selected;
	
	private static int counter = 1;
	private int id = 0;

	/**
	 * Creates a new letter with its points and value.
	 * 
	 * @param points
	 *            the points
	 * @param value
	 *            the value, which is upper case everytime.
	 * @param count
	 */
	public Letter(int points, char value, int count) {
		if (!Character.isLetter(value)) {
			throw new IllegalArgumentException("value must be a letter!");
		}

		if (Character.isLowerCase(value)) {
			this.value = Character.toUpperCase(value);
		} else {
			this.value = value;
		}

		if (points <= 0 || count <= 0) {
			throw new IllegalArgumentException("points and count must be > 0!");
		}

		this.points = points;
		this.count = count;
		
		id = counter++;
		
	}

	public int getPoints() {
		return points;
	}

	public char getValue() {
		return value;
	}

	public int getCount() {
		return count;
	}
	
	public int getId() {
		return id;
	}

	public void paint(Graphics2D g, int x, int y,  int fieldSize) {
		ColorTheme theme = ColorTheme.getInstance();
		
		if(selected) {
			g.setColor(theme.getColor("selectedStoneBgColor"));
		} else {
			g.setColor(theme.getColor("stoneBgColor"));			
		}

		g.fillRoundRect((2 * BoardPanel.FIELD_MARGIN) + x * fieldSize,
				(2 * BoardPanel.FIELD_MARGIN) + y * fieldSize, fieldSize
						- (3 * BoardPanel.FIELD_MARGIN), fieldSize
						- (3 * BoardPanel.FIELD_MARGIN),
				BoardPanel.FIELD_MARGIN * 2, BoardPanel.FIELD_MARGIN * 2);

		g.setColor(theme.getColor("stoneFgColor"));
		g.drawRoundRect((2 * BoardPanel.FIELD_MARGIN) + x * fieldSize,
				(2 * BoardPanel.FIELD_MARGIN) + y * fieldSize, fieldSize
						- (3 * BoardPanel.FIELD_MARGIN), fieldSize
						- (3 * BoardPanel.FIELD_MARGIN),
				BoardPanel.FIELD_MARGIN * 2, BoardPanel.FIELD_MARGIN * 2);

		String s = String.valueOf(getValue());
		Font font = new Font(Font.SANS_SERIF, Font.BOLD, fieldSize / 3 + 5);

		g.setFont(font);
		FontMetrics fm = g.getFontMetrics();
		g.drawString(s,
						(int) (BoardPanel.FIELD_MARGIN + x * fieldSize + (fieldSize - fm
								.stringWidth(s)) / 2),
						(int) (y * fieldSize + (fieldSize - fm.getHeight() / 2)));

		Font small = new Font(Font.SANS_SERIF, Font.PLAIN, fieldSize / 4);
		g.setFont(small);
		fm = g.getFontMetrics();
		s = String.valueOf(getPoints());
		g.drawString(s, (int) (BoardPanel.FIELD_MARGIN + x * fieldSize)
				+ fm.stringWidth(s), (int) (BoardPanel.FIELD_MARGIN + y
				* fieldSize)
				+ fm.getHeight());
	}

	public boolean isSelected() {
		return selected;
	}
	
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	@Override
	public Letter clone() {
		return new Letter(points, value, count);
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof Letter) {
			return id == ((Letter) o).id;
		} 
		
		return false;
	}
	
	@Override
	public String toString() {
		return String.valueOf(value) + " " + points + " " + count;
	}
}
